package sda.app_01;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;

import sda.app_01.fragment.fragment_1;
import sda.app_01.fragment.fragment_2;
import sda.app_01.fragment.fragment_gps;


public class MainActivity extends ActionBarActivity implements View.OnClickListener{

    // 基本的findviewbyid 绑定控件
    Toolbar toolbar ;


    // Afinal 框架 通过注解绑定 控件
    @ViewInject(id = R.id.viewpager)
    ViewPager viewPager ;

    @ViewInject(id = R.id.btn_1,click = "onClick")
    Button btn_1 ;

    @ViewInject(id = R.id.btn_2,click = "onClick")
    Button btn_2 ;

    @ViewInject(id = R.id.btn_3,click = "onClick")
    Button btn_3 ;

    @ViewInject(id = R.id.btn_4,click = "onClick")
    Button btn_4 ;



    // ViewPager adapter
    MyAdapter adapter ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);

        // Afinal 绑定必须执行
        FinalActivity.initInjectedView(this);


        // 5.0 特性   兼容包 V7
        setSupportActionBar(toolbar);

        // 调用 actionbar 的 默认返回按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);



        setUp();
    }

    /**
     *
     */
    private void setUp() {
        adapter = new MyAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            startActivity(new Intent(MainActivity.this,NavigationActivity.class));
            return true;
        }


        switch (id){
            case android.R.id.home:
                finish();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.btn_1:
                viewPager.setCurrentItem(0);
                break;
            case R.id.btn_2:
                viewPager.setCurrentItem(1);
                break;
            case R.id.btn_3:
                viewPager.setCurrentItem(2);
                break;
            case R.id.btn_4:
                viewPager.setCurrentItem(3);
                break;
        }
    }

    private class MyAdapter extends FragmentStatePagerAdapter{
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null ;
            switch (position){
                case 0:
                    fragment = new fragment_1();
                    break;
                case 3:
                    fragment = new fragment_gps();
                    break;
                default:
                    fragment = new fragment_2();
                    Bundle bundle = new Bundle();
                    bundle.putInt("position",position);
                    fragment.setArguments(bundle);
                    break;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 4;
        }
    }
}
